package action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import basic.SortInfo;
import common.BaseAction;
import common.dict.CommonDict;
import entities.TNotice;
import entities.TOperator;
import interfaces.INoticeService;
import interfaces.IOperatorService;
import util.ParameterUtil;
import utils.StringUtil;

public class NoticeAction extends BaseAction {

	private static final long serialVersionUID = 2053252004973147709L;

	private INoticeService noticeService;
	private IOperatorService operatorService;

	private TNotice noticeBean;
	private List<TNotice> noticeList;

	private String ntcNo;
	private String optId;
	private String ntcInfo;
	private String ntcRead;
	private String ntcState;
	private Date ntcDate;
	private String ntcRemark;
	private String ntcOpt;

	private Integer ntcCount;
	private Date ntcDateTop;
	private Date ntcDateEnd;

	// 新增或者修改入口
	// 根据主键获取对象,为空则创建带主键新对象
	public String edit() throws Exception {
		ntcOpt = "1";
		return SUCCESS;
	}

	// 更新或保存数据
	// 数据库内已存在该主键则执行修改,不存在则执行新增
	public String save() throws Exception {
		if (ntcOpt.equals("0")) {
			noticeBean = new TNotice();
			noticeBean.setNtcNo(StringUtil.guid());
			noticeBean.setOptId(optId);
			noticeBean.setNtcInfo(ntcInfo);
			noticeBean.setNtcRead("0");
			noticeBean.setNtcState("1");
			noticeBean.setNtcDate(new Date());
			noticeBean.setNtcRemark("");
			noticeService.persist(noticeBean);
			return SUCCESS;
		}

		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("optState", "1");

		querySet.setPageInfo(new PageInfo(1, Integer.MAX_VALUE));
		querySet.setParamMap(paramMap);

		ResultSet<TOperator> resultSet = operatorService.search(querySet);
		List<TOperator> listoperator = resultSet.getResultList();

		for (TOperator operator : listoperator) {
			noticeBean = new TNotice();
			noticeBean.setNtcNo(StringUtil.guid());
			noticeBean.setOptId(operator.getOptId());
			noticeBean.setNtcInfo(ntcInfo);
			noticeBean.setNtcRead("0");
			noticeBean.setNtcState("1");
			noticeBean.setNtcDate(new Date());
			noticeBean.setNtcRemark("");
			noticeService.persist(noticeBean);
		}
		return SUCCESS;
	}

	// 列表展示页面
	// paramMap为查询条件,sortInfo为排序方式,pageInfo为分页对象
	// paramMap可为空,sortInfo可为空,pageInfo不能为空,如果需要查询全部则设置pageIndex为1,pageSize为Integer.MAX_VALUE
	public String list() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("ntcNo", StringUtil.split(ntcNo));
		paramMap.put("optId", StringUtil.split(optId));
		paramMap.put("ntcInfo", ntcInfo);
		paramMap.put("ntcRead", ntcRead);
		paramMap.put("ntcDate", ParameterUtil.dateCombin(ntcDateTop, ntcDateEnd));

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "ntcDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TNotice> resultSet = noticeService.search(querySet);
		noticeList = resultSet.getResultList();
		pageInfo = resultSet.getPageInfo();
		return SUCCESS;
	}

	public String view() throws Exception {
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("optId", StringUtil.split(optId));
		paramMap.put("ntcRead", "0");
		ntcCount = noticeService.record(paramMap);

		QuerySet<Object> querySet = new QuerySet<Object>();

		paramMap = new HashMap<String, Object>();
		paramMap.put("optId", StringUtil.split(optId));
		paramMap.put("ntcRead", ntcRead);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "ntcDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(1, CommonDict.NOTICE_LIMIT));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TNotice> resultSet = noticeService.search(querySet);
		noticeList = resultSet.getResultList();
		return SUCCESS;
	}

	public String move() throws Exception {
		noticeService.remove(ntcNo);
		return SUCCESS;
	}

	/////////////////////////////////

	public INoticeService getNoticeService() {
		return noticeService;
	}

	public void setNoticeService(INoticeService noticeService) {
		this.noticeService = noticeService;
	}

	public TNotice getNoticeBean() {
		return noticeBean;
	}

	public void setNoticeBean(TNotice noticeBean) {
		this.noticeBean = noticeBean;
	}

	public List<TNotice> getNoticeList() {
		return noticeList;
	}

	public void setNoticeList(List<TNotice> noticeList) {
		this.noticeList = noticeList;
	}

	public String getNtcNo() {
		return ntcNo;
	}

	public void setNtcNo(String ntcNo) {
		this.ntcNo = ntcNo;
	}

	public String getNtcInfo() {
		return ntcInfo;
	}

	public void setNtcInfo(String ntcInfo) {
		this.ntcInfo = ntcInfo;
	}

	public String getNtcRead() {
		return ntcRead;
	}

	public void setNtcRead(String ntcRead) {
		this.ntcRead = ntcRead;
	}

	public String getNtcState() {
		return ntcState;
	}

	public void setNtcState(String ntcState) {
		this.ntcState = ntcState;
	}

	public Date getNtcDate() {
		return ntcDate;
	}

	public void setNtcDate(Date ntcDate) {
		this.ntcDate = ntcDate;
	}

	public String getNtcRemark() {
		return ntcRemark;
	}

	public void setNtcRemark(String ntcRemark) {
		this.ntcRemark = ntcRemark;
	}

	public Date getNtcDateTop() {
		return ntcDateTop;
	}

	public void setNtcDateTop(Date ntcDateTop) {
		this.ntcDateTop = ntcDateTop;
	}

	public Date getNtcDateEnd() {
		return ntcDateEnd;
	}

	public void setNtcDateEnd(Date ntcDateEnd) {
		this.ntcDateEnd = ntcDateEnd;
	}

	public String getNtcOpt() {
		return ntcOpt;
	}

	public void setNtcOpt(String ntcOpt) {
		this.ntcOpt = ntcOpt;
	}

	public IOperatorService getOperatorService() {
		return operatorService;
	}

	public void setOperatorService(IOperatorService operatorService) {
		this.operatorService = operatorService;
	}

	public String getOptId() {
		return optId;
	}

	public void setOptId(String optId) {
		this.optId = optId;
	}

	public Integer getNtcCount() {
		return ntcCount;
	}

	public void setNtcCount(Integer ntcCount) {
		this.ntcCount = ntcCount;
	}

}
